{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[22]:\n",
    "\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#########  百度语音合成库使用说明  #################\n",
    "# baidu_word_2_sound类提供：初始化方法和文字语音合成方法\n",
    "# init初始化方法：\n",
    "#        需要传入百度AI应用提供的APP_ID、API_KEY和SECRET_KEY\n",
    "# trans_word_to_sound语音合成方法： \n",
    "#        待合成的文字，注意用用单引号包裹文字，例如：'要合成的文字'\n",
    "#        合成后语音保存文件名，注意用用单引号包裹文字，例如：'auido.mp3'\n",
    "#################################################\n",
    "\n",
    "#########  调用方式举例  #################\n",
    "# from playsound import playsound\n",
    "# \n",
    "# \"\"\" 我的 APPID AK SK \"\"\"\n",
    "# APP_ID = '20114704'\n",
    "# API_KEY = 'Lwd56jic7ZvTon8PsYyZ5DfG'\n",
    "# SECRET_KEY = 'EnNdR4Vr0HgATxz7LiHEDx3dKfLkWFGj'\n",
    "# \n",
    "# if __name__ == \"__main__\":\n",
    "#   try:\n",
    "#         baidu_word_2_sound(APP_ID, API_KEY, SECRET_KEY)\n",
    "#         baidu_word_2_sound.trans_word_to_sound('淘淘，要听奶奶的话','sound.mp3')\n",
    "#        \n",
    "#       playsound('sound.mp3')\n",
    "#     except KeyboardInterrupt:\n",
    "#        print(\"任务被终止了\")\n",
    "#################################################        \n",
    "\n",
    "from aip import AipSpeech\n",
    "\n",
    "class baidu_word_2_sound(object):\n",
    "    \n",
    "    #百度AI应用提供参数\n",
    "    APP_ID      = None\n",
    "    API_KEY     = None\n",
    "    SECRET_KEY  = None\n",
    "    client      = None\n",
    "    \n",
    "    def __init__(self, app_id, api_key, secret_key):\n",
    "        #获取提供的百度AI接口参数\n",
    "        self.APP_ID = app_id\n",
    "        self.API_KEY = api_key\n",
    "        self.SECRET_KEY = secret_key\n",
    "        \n",
    "        #获取授权\n",
    "        self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)\n",
    "        \n",
    "    def trans_word_to_sound(self, word, sound_file, sound_speed = 5, \\\n",
    "                            sound_tune = 5, sound_volume = 5, sound_type = 0):\n",
    "        #进行语音合成\n",
    "        result  = self.client.synthesis(word, 'zh', 1, {\n",
    "            'spd' : sound_speed,'pit':sound_tune,'vol': sound_volume,'per':sound_type\n",
    "        })\n",
    "\n",
    "        # 识别正确返回语音二进制 错误则返回dict 参照下面错误码\n",
    "        if not isinstance(result, dict):\n",
    "            with open(sound_file, 'wb') as f:\n",
    "                f.write(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
